/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import connection.ConnectionDB;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Locations;

/**
 *
 * @author Administrator
 */
public class LocationsDAL {

    private ConnectionDB conn = new ConnectionDB();
    private CallableStatement cs;

    public boolean newLocation(Locations local) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_NewLocation(?,?)}");
            cs.setString("name", local.getLocationName());
            cs.setString("description", local.getDescription());

            if (cs.executeUpdate() == 1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(LocationsDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean updateLocation(Locations local) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_UpdateLocation(?,?,?)}");
            cs.setInt("locationID",local.getLocationID());
            cs.setString("name", local.getLocationName());
            cs.setString("description", local.getDescription());

            if (cs.executeUpdate() == 1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(LocationsDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public List<Locations> listAllLocation(){
        try {
            List<Locations> list = new ArrayList<Locations>();
            Locations local;
            cs = conn.createConnection().prepareCall("{call sp_ListAllLocations}");
            ResultSet rs = cs.executeQuery();

            while(rs.next()){
                local = new Locations(rs.getInt("LocationID"),rs.getString("LocationName"),rs.getString("Description"));
                list.add(local);
            }
            return list;
        } catch (SQLException ex) {
            Logger.getLogger(LocationsDAL.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
